layui.define(['baseUrl', 'axios', 'store', 'qs','utils'], function (exports) {
	var ARES_HTTP = {},
		baseUrl = layui.baseUrl,
		axios = layui.axios,
		store = layui.store,
		qs = layui.qs,
        utils = layui.utils;

	/**
	 * @author: ares
	 * @date: 2020/5/31 19:34
	 * @description: http请求实例
	 */
	var _axios = axios.create({
		baseURL: baseUrl,
		withCredentials: true,
		crossDomain: true,
		data: {},
		params: {},
        headers: {"Content-Type": "application/json;charset=UTF-8;"}
	});

	/**
	 * @author: ares
	 * @date: 2020/5/31 19:34
	 * @description: axios上传文件实例
	 */
	var _upload = axios.create({
		baseURL: baseUrl,
		withCredentials: true,
		crossDomain: true,
		params: {},
		headers: {"Content-Type": "multipart/form-data"}
	});

	/**
	 * @author: ares
	 * @date: 2020/5/31 19:34
	 * @description: axios下载文件实例
	 */
	var _download = axios.create({
		baseURL: baseUrl,
		withCredentials: true,
		crossDomain: true,
		params: {},
		// headers: {"Content-Type": "application/force-download;charset=UTF-8;"},
		responseType: "blob",
		onUploadProgress: function (progressEvent) {
			// Do whatever you want with the native progress event
		},
	});

	/**
	 * @author: ares
	 * @date: 2020/5/31 19:33
	 * @description: 请求成功拦截器
	 * @param {object}request  请求信息
	 * @returns {*}
	 */
	function requestSuccessInterceptors(request) {
		var token = store.get('token');
        if(Object.prototype.toString.apply(request.params) == '[object Object]'){
            request.params._t = +new Date;
            request.params.token = token;
        }
        if(Object.prototype.toString.apply(request.data) == '[object Object]'){
            request.data.token = token;
        }
		return request;
	};

	/**
	 * @author: ares
	 * @date: 2020/5/31 19:33
	 * @description: 请求出错处理器
	 * @param {object}error 请求错误信息
	 * @returns {Promise.<*>}
	 */
	function requestErrorInterceptors(error) {
		return Promise.reject(error);
	};

	/**
	 * @author: ares
	 * @date: 2020/5/31 19:31
	 * @description: 响应成功拦截器
	 * @param {object}res  请求响应
	 * @returns {*}
	 */
	function responseSuccessInterceptors(res) {
		var response = res.data,
			code = response.code
				|| (response.data && response.data.code),
			operate = response.operate
				|| (response.data && response.data.operate),
			msg = response.message
				|| (response.data && response.data.message)
				|| response.msg
				|| (response.data && response.data.msg);
		if (res.status != 200) {
			return Promise.reject(msg);
		} else {
			if (code == 2000 || code == 200 || operate === 'success' || operate === 'SUCCESS' || msg === 'success' || msg === 'SUCCESS') {
				return Promise.resolve(response);
			} else {
				return Promise.reject(msg);
			}
		}
	};

	/**
	 * @author: ares
	 * @date: 2020/5/31 19:32
	 * @description: 响应出错处理器
	 * @param {object}error  响应出错信息
	 * @returns {Promise.<*>}
	 */
	function responseErrorInterceptors(error) {
        let {response} = error,
            msg = response.data.message || error.message;
        return Promise.reject(msg);
	};

	/**
	 * @author: ares
	 * @date: 2020/5/31 19:30
	 * @description: http请求及响应拦截
	 */
	_axios.interceptors.request.use(
		requestSuccessInterceptors,
		requestErrorInterceptors
	);
	_axios.interceptors.response.use(
		responseSuccessInterceptors,
		responseErrorInterceptors
	);

	/**
	 * @author: ares
	 * @date: 2020/5/31 19:30
	 * @description: 上传请求及响应拦截
	 */
	_upload.interceptors.request.use(
		requestSuccessInterceptors,
		requestErrorInterceptors
	);
	_upload.interceptors.response.use(
		responseSuccessInterceptors,
		responseErrorInterceptors
	);

	/**
	 * @author: ares
	 * @date: 2020/5/31 19:27
	 * @description: 错误处理
	 * @param {string}errMsg
	 * @param {boolean}cancelErrorTip 是否取消默认错误提示
	 */
	function errorHandler(errMsg, cancelErrorTip) {
		console.error(">>>接口出错<<<", errMsg);
        if (typeof errMsg == 'string' && errMsg.toLowerCase().indexOf('token') >= 0) {
			top.location = '/';
		} else {
			if (cancelErrorTip || !errMsg) return;
			utils.msgError(errMsg);
		}
	};

	/**
	 * @author: ares
	 * @date: 2020/5/31 19:28
	 * @description: http请求
	 * @param {object}reqServe   实际用到的axios实例
	 * @param {string}url      请求地址
	 * @param {object}config   请求配置
	 * @param {boolean}cancelErrorTip 是否取消默认错误提示
	 * @returns {Promise.<TResult>}
	 */
	function request(reqServe, url, config, cancelErrorTip) {
		if(config && config.urlencoded && (config.method == 'post' || config.method == 'put')){
			config.headers = { 'content-type': 'application/x-www-form-urlencoded' };
            if(config.data) config.data = qs.stringify(config.data);
		}
		return reqServe(url, config)
			.then(function (res) {
				return Promise.resolve(res);
			})
			.catch(function (err) {
				errorHandler(err, cancelErrorTip);
				return Promise.reject(err);
			});
	};

	/**
	 * @author: ares
	 * @date: 2018/8/20 20:17
	 * @description: http请求
	 * @param {string}url   请求地址
	 * @param {object}config 请求配置
	 * @param {boolean}cancelErrorTip 是否取消默认错误提示
	 * @returns {Promise.<TResult>}
	 */
	ARES_HTTP.service = function (url, config, cancelErrorTip) {
		return request(_axios, url, config, cancelErrorTip);
	};

	/**
	 * @author: ares
	 * @date: 2020/5/31 19:29
	 * @description: 上传
	 * @param {string}url   请求地址
	 * @param {object}config 请求配置
	 * @param {boolean}cancelErrorTip 是否取消默认错误提示
	 * @returns {Promise.<TResult>}
	 */
	ARES_HTTP.upload = function (url, config, cancelErrorTip) {
		return request(_upload, url, config, cancelErrorTip);
	};

	/**
	 * @author: ares
	 * @date: 2020/5/31 19:29
	 * @description: 下载
	 * @param {string}url   请求地址
	 * @param {object}config 请求配置
	 * @param {boolean}cancelErrorTip 是否取消默认错误提示
	 * @returns {Promise.<TResult>}
	 */
	ARES_HTTP.download = function (url, config, cancelErrorTip) {
		return request(_download, url, config, cancelErrorTip);
	};
	exports('http', ARES_HTTP);
});
